
//
//  NSString+Regex.m
//  SmartEstateCC
//
//  Created by 初唐科技 on 2018/3/6.
//  Copyright © 2018年 pretang. All rights reserved.
//

#import "NSString+Regex.h"

@implementation NSString (Regex)

- (NSString *)encryptPhone
{
    if (self.isPhone) {
        return [self stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    }
    return self;
}

- (BOOL)isPhone
{
    NSString *regex = @"^1\\d{10}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

- (BOOL)isCharacterAndNumberAtLeast:(int)least most:(int)most
{
    BOOL result = NO;
    if (self.length >= least){
        // 判断长度大于8位后再接着判断是否同时包含数字和字符
        NSString * regex = [NSString stringWithFormat:@"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{%d,%d}$", least, most];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [predicate evaluateWithObject:self];
    }
    return result;
}

- (BOOL)isNumber
{
    NSString *regex = @"^[0-9]+(\\.[0-9]+)?$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

- (BOOL)isInteger
{
    NSString *regex = @"^[0-9]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

- (BOOL)isCharacterOrNumberMost:(int)most
{
    NSString *regex = [NSString stringWithFormat:@"^[A-Za-z0-9]{1,%d}$",most];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

@end
